-- ========================================================
-- @File	: cookie.lua
-- @Brief	: 浏览器缓存
-- @Author	: Leo Zhao
-- @Date	: 2016-05-05
-- ========================================================
cookie = cookie or {}

-- 设置cookie
function cookie.set(key, value, expire, path)
	local s = "Set-cookie:" .. key .. '=' .. value .. ';';
	
	if expire then
		s = s .. 'expires=' .. time(os.time() + expire) .. ';';
	end

	s = s .. 'path=' .. (path or '/');
	header(s);
	cookie.parsed[key] = value;
end

-- 取cookie
function cookie.get(key)
	return cookie.parsed[key];
end

-- 清除cookie
function cookie.clear()
	for k, v in pairs(cookie.parsed) do
		cookie.set(k, '', -1);
	end
	cookie.parsed = {};
end

-- 打开cookie
function cookie.open()
	local t = {};
	local finder = split(_COOKIE or '', ';');

	for _, p in ipairs(finder) do
		local _1, _2, k, v = string.find(p, "([%w_]+)=(.+)");
		if k and v then t[k] = v end;
	end

	cookie.parsed = t;
end